
<?php
function itemIndexArray($headCategory, $location, $subCategory,$priceSort) {


$hostname="localhost";
$username="root";
$password="";
$database="db_dinkonst";
$i=0;

mysql_connect($hostname,$username,$password);
mysql_select_db($database) or die( "Unable to select database");


if (isset($location) && $location != NULL) {$extraQuery = "and items.user  = users.user_id and users.lan = '$location'";
                       $extraTable =", users";//To avoid wierd answers
                        }

if ($subCategory != NULL){ $subcategoryQuery= "and subCategory.subCategoryName = '$subCategory'";}
// Var tvungen att ändra denna till != Null, eftersom den tydligen alltid är isset.
// SORT BÖRJAR HÄR
if ($headCategory != NULL){ $headCategoryQuery= "and category.categoryName = '$headCategory'";}


if ($priceSort == NULL){$order="order by item_id DESC";}
if ($priceSort == 1) {$order="order by price";}
if ($priceSort == 2) {$order="order by price DESC";}

$result = mysql_query("select items.item_id from items, item_subCategory, subCategory, category $extraTable
                           where
                           items.item_id = item_subCategory.item
                           $extraQuery
                           and item_subCategory.subCategory = subCategory.subCategory_id
                           and subCategory.parentCategoryID = category.category_id
                           $headCategoryQuery
                           $subcategoryQuery
                           $order");

                 while($row = mysql_fetch_assoc($result))  {
                        $itemId = $row["item_id"];
                        $itemIdArray[$i] = $itemId;
                        ++$i;
                        }

                return ($itemIdArray);
}
//$category = 'Tavlor';
//$location = 'Vastmanland';
//$subCategory ='Oljemalning';
//$itemIdArray= itemIndexArray($category, $location, $subCategory);
//print_r($itemIdArray);

function picIndexArray() {



$hostname="localhost";
$username="root";
$password="";
$database="db_dinkonst";
$i=0;
//Connnects to database
mysql_connect($hostname,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
         $result = mysql_query("select item_id from items order by item_id DESC");

                 while($row = mysql_fetch_assoc($result))  {
                        $itemId = $row["item_id"];
                        $itemIdArray[$i] = $itemId;
                        ++$i;
                        }

return ($itemIdArray);
}
//Jag Skriver in lite fler funktioner här eftersom de används i samma fil

function getItemPrice($item) {
                 include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select price from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $price = $row['price'];
return $price;


}
function getItemSize($item) {
                 include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select width from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $width = $row['width'];


             $result = mysql_query("select height from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $height = $row['height'];
return "$width x $height";

}

function getItemTitle($item) {
                 include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select title from items where item_id = $item");
             $row = mysql_fetch_array($result);
             $title = $row['title'];
return $title;
}


function getItemDescription($item) {
                 include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select description from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $description = $row['description'];
return $description;


}

function getItemUsername($item) {
             include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select u.username from users u, items i where u.user_id = i.user and i.item_id = $item" );
             $row = mysql_fetch_array($result);
             $username = $row['username'];
return $username;


}
function getItemHeight($item) {
                 include '../phpFunctions/databaseConnect.php';


             $result = mysql_query("select height from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $height = $row['height'];
return $height;

}
function getItemWidth($item) {
                 include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select width from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $width = $row['width'];



return $width;

}
function getItemEmail($item) {
             include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select u.email from users u, items i where u.user_id = i.user and i.item_id = $item" );
             $row = mysql_fetch_array($result);
             $email = $row['email'];
return $email;
}

function getItemLocation($item) {
             include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select u.lan from users u, items i where u.user_id = i.user and i.item_id = $item" );
             $row = mysql_fetch_array($result);
             $lan = $row['lan'];
return $lan;
}

function getItemUploadDate($item) {
                 include '../phpFunctions/databaseConnect.php';


             $result = mysql_query("select timestamp from items where item_id = $item" );
             $row = mysql_fetch_array($result);
             $timestamp = $row['timestamp'];
return $timestamp;

}
function rand_except($min, $max, $except)
  //function returns a random integer between min and max, just like function rand() does.
  // Difference to rand is that the random generated number will not use any of the values
  // placed in $except. ($except must therefore be an array)
  // function returns false if $except holds all values between $min and $max.
  {
  //first sort array values
  sort($except, SORT_NUMERIC);
  //calculate average gap between except-values
  $except_count = count($except);
  $avg_gap = ($max - $min + 1 - $except_count) / ($except_count + 1);
  if ($avg_gap <= 0)
     return false;
  //now add min and max to $except, so all gaps between $except-values can be calculated
  array_unshift($except, $min - 1);
  array_push($except, $max + 1);
  $except_count += 2;
  //iterate through all values of except. If gap between 2 values is higher than average gap,
  // create random in this gap
  for ($i = 1; $i < $except_count; $i++)
      if ($except[$i] - $except[$i - 1] - 1 >= $avg_gap)
         return mt_rand($except[$i - 1] + 1, $except[$i] - 1);
  return false;
  }
function getUserInfo($userName, $info){

     include '../phpFunctions/databaseConnect.php';

             $result = mysql_query("select $info from users where username ='$userName'" );
             $row = mysql_fetch_array($result);
             $info = $row[$info];
return $info;


}



?>
